---
title: 快速上手
---

:::info
开始前请遵照 [安装指南](./installation) 步骤并成功安装 Derrick CLI。
:::

## 1. 展示可用的 Riggings

```bash
$ derrick list

Available riggings:
	golang
	java
	nodejs
	php
	python
```

## 2. 容器化一个 Java 应用

首先 git clone 一个示例应用:

```bash
git clone https://github.com/hongchaodeng/simple-java-maven-app.git
cd simple-java-maven-app
```

然后用 Derrick 来自动生成容器化配置:

```bash
derrick gen
```

输出如下:

```bash
Successfully detected your platform is 'java'
Successfully generated: Dockerfile
Successfully generated: chart/Chart.yaml
Successfully generated: chart/.helmignore
Successfully generated: chart/templates/NOTES.txt
Successfully generated: chart/templates/deployment.yaml
Successfully generated: chart/templates/_helpers.tpl
Successfully generated: chart/templates/hpa.yaml
Successfully generated: chart/templates/ingress.yaml
Successfully generated: chart/templates/service.yaml
Successfully generated: chart/templates/serviceaccount.yaml
Successfully generated: chart/templates/tests/test-connection.yaml
Successfully generated: chart/values-dev.yaml
Successfully generated: chart/values-prod.yaml
Successfully generated: chart/values.yaml
Successfully generated: kubernetes/dev/deployment.yaml
Successfully generated: kubernetes/prod/deployment.yaml
Successfully generated: kustomize/base/deployment.yaml
Successfully generated: kustomize/base/kustomization.yaml
Successfully generated: kustomize/overlays/dev/deployment.yaml
Successfully generated: kustomize/overlays/dev/kustomization.yaml
Successfully generated: kustomize/overlays/prod/deployment.yaml
Successfully generated: kustomize/overlays/prod/kustomization.yaml
Successfully generated: derrick.json
```

这里我们把输出样例也上传到 Github 上以供参考：

- [Dockerfile](https://github.com/hongchaodeng/simple-java-maven-app/blob/master/Dockerfile)
- [chart](https://github.com/hongchaodeng/simple-java-maven-app/tree/master/chart)
- [kustomize](https://github.com/hongchaodeng/simple-java-maven-app/tree/master/kustomize)
- [kubernetes](https://github.com/hongchaodeng/simple-java-maven-app/tree/master/kubernetes)


下面我们来深入解读生成的配置。

### Dockerfile

生成的 Dockerfile 如下:

```Dockerfile
# First stage: build environment
FROM maven:3.5.0-jdk-8-alpine AS builder

# To resolve dependencies first without re-download everytime
ADD ./pom.xml pom.xml
# By default mvn use '~/.m2' which could be cleaned up, change to use './.m2'
RUN mvn install -Dmaven.repo.local=./.m2

ADD ./src src/
# package jar
RUN mvn install -Dmaven.test.skip=true -Dmaven.repo.local=./.m2

# Second stage: runtime environment
From openjdk:15

# copy jar from the first stage
COPY --from=builder target/my-app-1.0-SNAPSHOT.jar my-app-1.0-SNAPSHOT.jar

# MY_CPU_LIMIT could be imported via downward API automatically in Kubernetes Deployment.
CMD ["java", \
  "-XX:InitialRAMPercentage=75", \
  "-XX:MaxRAMPercentage=75", \
  "-XX:MinRAMPercentage=25", \
  "-XX:ActiveProcessorCount=$MY_CPU_LIMIT", \
  "-jar", "my-app-1.0-SNAPSHOT.jar"]
```

Derrick 做了如下优化:

- 分成了 build 和 runtime 两个 stages。这样 runtime image 不会引入 build 的内容，缩小最终部署的 image size。
- 使用了社区广受欢迎和支持良好的 `openjdk` 作为 runtime 的 base image。
- 优化了依赖的缓存。这样当重新构建时无需重复安装已缓存的依赖。
- 从 pom.xml 中自动解析 artifact name。

### Kubernetes, Helm, Kustomize

Derrick 自动构建了用于部署到 Kubernetes 的配置文件，有以下目录：

```
kubernetes/
chart/
kustomize/
```

下面我们来展示下 helm chart 和 kustomize 的输出:

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

<Tabs
  className="unique-tabs"
  defaultValue="helm"
  values={[
    { label: "Helm Chart", value: "helm" },
    { label: " Kustomize", value: "kustomize" },
  ]}>

<TabItem value="helm">

对于 helm chart，可以运行以下命令来查看输出:

```bash
cd chart/
helm template test .
```

得到输出如下:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-simple-java-maven-app
  labels:
    helm.sh/chart: simple-java-maven-app-0.1.0
    app.kubernetes.io/name: simple-java-maven-app
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: simple-java-maven-app
      app.kubernetes.io/instance: test
  template:
    metadata:
      labels:
        app.kubernetes.io/name: simple-java-maven-app
        app.kubernetes.io/instance: test
    spec:
      serviceAccountName: test-simple-java-maven-app
      containers:
        - name: simple-java-maven-app
          image: "your-image"
          imagePullPolicy: IfNotPresent
          env:
            - name: MY_CPU_REQUEST
              valueFrom:
                resourceFieldRef:
                  containerName: java-app
                  resource: requests.cpu
            ...
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 80
            ...
          readinessProbe:
            ...
          resources:
            limits:
              cpu: 2
              memory: 1500M
            requests:
              cpu: 1
              memory: 1500M
```

</TabItem>

<TabItem value="kustomize">

对于 Kustomize，可以运行以下命令来查看输出:

```bash
cd kustomize
kustomize build kustomize/overlays/prod
```

得到输出如下:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: simple-java-maven-app
    tier: production
  name: prod-simple-java-maven-app
spec:
  selector:
    matchLabels:
      app: simple-java-maven-app
      tier: production
  template:
    metadata:
      labels:
        app: simple-java-maven-app
        tier: production
    spec:
      containers:
      - ...
        image: your-image
        name: java-app
        resources:
          limits:
            cpu: 2
            memory: 2000M
          requests:
            cpu: 1
            memory: 1500M
```

</TabItem>

</Tabs>

:::note
生成的部署文件可以帮助你快速上手容器化配置。但真正部署起来还需要你去构建镜像，并把镜像名等填入对应的字段中。
如果你使用[云中间件](/docs/guides/middleware) 如 AHAS/MSE/ARMS 等，还需要在 annotations 中填入 app-name。
:::

### 多环境管理

Derrick 默认提供了 dev/prod 两个环境的部署模板。利用 helm/kustomize 的能力可以更好地对不同环境下的配置进行更好地重用和管理。
